package com.horoscope.astrology.zodiac.signs.free.fragment

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.horoscope.astrology.zodiac.signs.free.AppSettingManager
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.activity.*
import com.horoscope.astrology.zodiac.signs.free.adapter.AstrologyMainAdapter
import com.horoscope.astrology.zodiac.signs.free.bean.ContentBean
import com.horoscope.astrology.zodiac.signs.free.bean.UserBean
import com.horoscope.astrology.zodiac.signs.free.infc.OnItemClickedListener
import com.horoscope.astrology.zodiac.signs.free.utils.AstrologyDecoration
import com.horoscope.astrology.zodiac.signs.free.utils.EventType
import com.horoscope.astrology.zodiac.signs.free.utils.FlurryEventUtil
import com.horoscope.astrology.zodiac.signs.free.utils.ad.ad_business.ADNativeManager

/**
 * 占星术
 * */
class AstrologyFragment : BaseFragment() {

    private var mUserBean: UserBean? = null
    lateinit var recyclerView1: RecyclerView
    lateinit var adapter1: AstrologyMainAdapter
    private val mDataList = ArrayList<ContentBean>()
//    "Numerology",
//    "Palm Scan"
    private val textList = arrayListOf<String>(
        "Personal birth\nchat",
        "Love &\nFriendship",
        "Love Zodiac\ncompatibility",
        "Zodiac\ncompatibility",
        "Name\ncompatibility",
        "Quizzes",
        "Numerology"
    )

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_astrology_new, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val nativeAdBean = ADNativeManager.mInstance.getNativeAdBean("Native_Astrology")
        mUserBean = AppSettingManager.readObject("user_info")
        recyclerView1 = view.findViewById(R.id.rl_astrology)
        textList.forEach {
            val bean = ContentBean()
            bean.title = it
            mDataList.add(bean)
        }
        // 是否有可用Ad
        if (nativeAdBean != null) {
            if (nativeAdBean.facebookNativeAD != null || nativeAdBean.googleNativeAd != null) {
                val bean = ContentBean()
                bean.title = "Ad"
                bean.nativeAdBean = nativeAdBean
                mDataList.add(3, bean)
            }
        }

        adapter1 = AstrologyMainAdapter(activity, mDataList)
        initListeners()
        recyclerView1.adapter = adapter1
        recyclerView1.layoutManager = GridLayoutManager(activity, 2)
        recyclerView1.addItemDecoration(AstrologyDecoration(10f, 10f))

    }

    private fun initListeners() {
        adapter1.setOnItemClickedListener(object : OnItemClickedListener {
            override fun onItemClick(view: View, position: Int, string: String) {
                when (string) {
                    textList[0] -> {// 出生星盘【天宫图】
                        FlurryEventUtil.putDataToEvent(
                            EventType.TYPE_ASTROLOGY_CLICK,
                            "type",
                            "Personal_Birth_Chat"
                        )
                        val astrologyToProfile =
                            Intent(activity, InputProfileActivity::class.java)
                        astrologyToProfile.putExtra("fromContext", "AstrologyFragment")
                        startActivity(astrologyToProfile)
                    }
                    textList[1] -> { // 进入了关系匹配的结果页
                        FlurryEventUtil.putDataToEvent(
                            EventType.TYPE_ASTROLOGY_CLICK,
                            "type",
                            "Friendly_Relationship"
                        )
                        startActivity(
                            Intent(
                                activity,
                                RelationCompatibilityActivity::class.java
                            )
                        )
                    }
                    textList[2] -> {// 爱情线匹配度
                        FlurryEventUtil.putDataToEvent(
                            EventType.TYPE_ASTROLOGY_CLICK,
                            "type",
                            "Love_Compatibility"
                        )
                        startActivity(
                            CompatibilityActivity.generateIntent(
                                activity,
                                CompatibilityActivity.TYPE_LOVE
                            )
                        )

                    }
                    textList[3] -> {// 生肖匹配度
                        FlurryEventUtil.putDataToEvent(
                            EventType.TYPE_ASTROLOGY_CLICK,
                            "type",
                            "Zodiac_Compatibility"
                        )
                        startActivity(
                            CompatibilityActivity.generateIntent(
                                activity,
                                CompatibilityActivity.TYPE_ZODIAC
                            )
                        )

                    }
                    textList[4] -> {// 姓名匹配度
                        FlurryEventUtil.putDataToEvent(
                            EventType.TYPE_ASTROLOGY_CLICK,
                            "type",
                            "Name_Compatibility"
                        )
                        startActivity(Intent(activity, NameCompatibilityActivity::class.java))

                    }
                    textList[5] -> {// 小测试【最新 or 最热】
                        FlurryEventUtil.putDataToEvent(
                            EventType.TYPE_ASTROLOGY_CLICK,
                            "type",
                            "Quizzes"
                        )
                        startActivity(QuizzesActivity.generateIntent(activity))

                    }
                    textList[6] -> {
                        FlurryEventUtil.putDataToEvent(
                            EventType.TYPE_ASTROLOGY_CLICK,
                            "type",
                            "Numerology"
                        )
                        startActivity(Intent(activity, FourNumberActivity::class.java))
                    }
//                    textList[7] -> {
//                        FlurryEventUtil.putDataToEvent(
//                            EventType.TYPE_ASTROLOGY_CLICK,
//                            "type",
//                            "PalmScan"
//                        )
//                        startActivity(Intent(activity, PalmScanResultActivity::class.java))
//                    }
                }
            }
        })

        /*LiveEventBus.get("AstrologyPosition", Int::class.java)
            .observeSticky(this, Observer<Int?> { t ->
                t?.let {
                    when (t) {
                    }
                }
            })*/
    }

}